package cn.melon.mod.store.entity;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @ClassName: Goods
 * @author he
 *  Goods的实体类
 */
public class Goods extends GoodsInfo {
	
	private String goodsId;
	
	/**
	 * 缺省构造
	 */
	public Goods() {
	}
	
	public Goods(String goodsID,String goodsName, String goodsType, double goodsPrice, String goodsDes, long goodsSales,
			boolean isSoldOut) {
		super(goodsName, goodsType, goodsPrice, goodsDes, goodsSales, isSoldOut);
		
		setGoodsId(goodsID);
	}

	public String getGoodsId() {
		return goodsId;
	}

	public void setGoodsId(String goodsId) {
		this.goodsId = goodsId;
	}
	
	/**
	 * 用json格式的字符串来设置Goods信息
	 * @param jsonObject void
	 */
	public void setGoodsByJsonObj(JSONObject jsonObject){
	    try {
            this.setGoodsId(jsonObject.getString("GoodsId"));
            this.setGoodsName(jsonObject.getString("GoodsName"));
            this.setGoodsType(jsonObject.getString("GoodsType"));
            this.setGoodsPrice(jsonObject.getDouble("GoodsPrice"));
            this.setGoodsDescription(jsonObject.getString("GoodsDescription"));
            this.setGoodsSales(jsonObject.getLong("GoodsSales"));
            this.setSoldOut(jsonObject.getBoolean("isSoldOut"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
	}
	
	/**
	 * 将Goods对象转化为json对象
	 * @return JSONObject 返回jsond对象
	 */
	public JSONObject toJsonObject(){
		JSONObject jsonObject = new JSONObject();
		
		try {
			jsonObject.put("GoodsId", this.getGoodsId());
			jsonObject.put("GoodsName", this.getGoodsName());
			jsonObject.put("GoodsType", this.getGoodsType());
			jsonObject.put("GoodsPrice", this.getGoodsPrice());
			jsonObject.put("GoodsDescription", this.getGoodsDescription());
			jsonObject.put("GoodsSales", this.getGoodsSales());
			jsonObject.put("isSoldOut", this.isSoldOut());
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return jsonObject;
	}
}
